import Vue from 'vue';
import Vuex from 'vuex'
Vue.use(Vuex);
import Axios from 'axios';
var cookieparser = require('cookieparser')
var Cookie = require("js-cookie")

var serverUrl = "https://api.fengwotuan.com"
var ajaxUrl
var isDev = false
if (process.env.NODE_ENV == 'production') {
  serverUrl = "https://api.fengwotuan.com"
  isDev = false
} else {
  serverUrl = "https://api.fengwotuan.com"
  // serverUrl = "http://127.0.0.1:4001"
  isDev = true
}
ajaxUrl = serverUrl + "/a"



const createStore = () => {

  return new Vuex.Store({
    strict: !isDev,
    state: {
      merId: "",
      mer: {},
      lang: "cn"
    },
    mutations: {
      ajax(state, o) {
        var url2
        if (process.client && !isDev) {
          url2 = "/a"
        } else {
          url2 = ajaxUrl
        }
        // console.log(process.client);
        // console.log(isDev);
        // console.log(url2);
        // console.log(o);

        Axios.post(url2, {
          class: o.class,
          para: o.para,
          j: o.j,
          merId: state.merId,
          source: state.sourceId,
          sourceName: state.sourceName

        }).then(async (res) => {
          o.fun(res)
        })
      },

      error(state, o) {
        state.error.message = o.message
      },
      getSource(state, o) {
        state.sourceId = "website"
        state.sourceName = "客户网站"
      },
      setMer(state, o) {
        state.mer = o.mer
        state.merId = o.mer._id
        state.lang = o.mer.lang

      }

    },
    actions: {
      ajax({
        commit,
        state
      }, o) {
        return new Promise((resolve, reject) => {
          Axios.post(ajaxUrl, {
            class: o.class,
            para: o.para,
            j: o.j,
            merId: state.merId,
          }).then((res) => {
            resolve(res)
          })
        });

      },
      async nuxtServerInit({
        commit,
        dispatch
      }, {
        req
      }) {

        var host = req.headers.host
        var cnMerId = "5b02900e0630e88136b075e6"
        var enMerId = "5cb545333d074e31e5717d62"
        var merId = ""
        if (process.env.NODE_ENV == "development") {
          merId = cnMerId //如果在开发环境,直接赋值 cnMerId 就是中文  enMerId 就是英文
        } else {
          var d2 = host.split(".")[0]
          if (d2 == "en") {
            merId = enMerId
          } else {
            merId = cnMerId
          }
        }
        var r = await dispatch("ajax", {
          class: "mer",
          para: "getMer",
          j: {
            _id: merId,
            options: {
              fields: {
                web: 1,
                lang: 1,
                name: 1,
                no: 1
              }
            }
          },

        })
        //  console.log(r.data.info);
        //  console.log(r.data);
        commit("setMer", {
          mer: r.data.info
        })


        // var parsed = {}
        // if (req.headers.cookie) {
        //   parsed = cookieparser.parse(req.headers.cookie)
        //   var me = {}
        //   me.tokenId = parsed.tokenId;
        //   me.pic = parsed.pic;
        //   me.name = parsed.name
        //   me.memberId = parsed.memberId
        //   me.openId = parsed.openId
        //   commit('increment', me)
        // }
        //   console.log(req);





      }
    }
  })
}

export default createStore
